import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';

import '../../api/UserApi.dart';
import '../../commponets/ItemView.dart';
import '../../home/AppLocale.dart';
import '../../home/LocaleProvider.dart';
import '../../routers/fluro_navigator.dart';
import '../../tools/widgets/my_app_bar.dart';
import '../../utils/toast_utils.dart';
import '../login/login_router.dart';
import '../provider/CustomBottomPicker.dart';
import 'package:restart_app/restart_app.dart';


import 'model/UserModel.dart';

class UserSystemPage extends StatefulWidget {
  const UserSystemPage({super.key});

  @override
  State<UserSystemPage> createState() => _UserSystemPageState();
}

class _UserSystemPageState extends State<UserSystemPage> {
  UserModel? _userModel;
  late int _userId;
  final UserApi _userApi = UserApi();

  @override
  void initState() {
    super.initState();
    // 可以在这里初始化用户数据
  }

  Future<void> _logout() async {
    try {
      final response = await _userApi.logout();
      if (response.code == 200) {
        setState(() {
          _userModel = null;
        });
        NavigatorUtils.push(context, LoginRouter.loginPage);
      } else {
        Toast.show("${AppLocale.text('error')}: ${response.code}");
      }
    } catch (e) {
      Toast.show("${AppLocale.text('error')}: $e");
    }
  }

  // 清理缓存方法
  void _clearCache() {
    // 实现清理缓存的逻辑
    Toast.show(AppLocale.text('success'));
  }

  // 重启应用方法 - 使用新插件
  void _restartApp() {
    Restart.restartApp(
    );
  }


  @override
  Widget build(BuildContext context) {
    final localeProvider = Provider.of<LocaleProvider>(context, listen: true);

    return Scaffold(
      appBar: MyAppBar(
        centerTitle: AppLocale.text('systemSettings'),
        backgroundColor: Colors.transparent,
        dropdownWidth: 140,
        dropdownBackgroundColor: Colors.white,
        dropdownBorderRadius: 20,
      ),
      body: Container(
          child: Column(
            children: [
              const SizedBox(height: 20),

              ItemView(
                width: 360,
                height: 50,
                leftPadding: 10,
                rightPadding: 20,
                leftWidget: Row(
                  children: [
                    const SizedBox(width: 10),
                    Text(
                      AppLocale.text('clearCache'),
                      style: const TextStyle(
                          fontSize: 15.0,
                          color: Colors.black
                      ),
                    ),
                  ],
                ),
                rightWidget: Row(
                  children: [
                    Text(
                      '23.2MB',
                      style: TextStyle(
                          fontSize: 14.0,
                          color: Colors.black54
                      ),
                    ),
                  ],
                ),
                borderRadius: 8,
                onTap: _clearCache,
              ),

              ItemView(
                width: double.infinity,
                height: 50,
                leftPadding: 10,
                rightPadding: 20,
                leftWidget: Row(
                  children: [
                    const SizedBox(width: 10),
                    Text(
                      AppLocale.text('languageSwitch'),
                      style: const TextStyle(
                        fontSize: 15.0,
                        color: Colors.black,
                      ),
                    ),
                  ],
                ),
                borderRadius: 8,
                onTap: () {
                  int currentLanguageIndex = localeProvider.isChinese ? 0 : 1;

                  List<Map<String, dynamic>> languageList = [
                    {'label': AppLocale.text('chinese'), 'value': 0},
                    {'label': AppLocale.text('english'), 'value': 1},
                  ];

                  showModalBottomSheet(
                    context: context,
                    backgroundColor: Colors.transparent,
                    builder: (BuildContext dialogContext) {
                      return CustomBottomPicker(
                        title: AppLocale.text('languageSwitch'),
                        height: 300,
                        list: languageList,
                        selectedColor: Colors.blue,
                        backgroundColor: Colors.white,
                        initialValue: currentLanguageIndex,
                        selectedShapeType: SelectedShapeType.hollowCircle,
                        onSelect: (newValue) async {
                          // 保存语言设置
                          if (newValue == 0) {
                            localeProvider.switchLocale('zh');
                          } else {
                            localeProvider.switchLocale('en');
                          }

                          // 显示提示信息
                          Toast.show(AppLocale.text('my_chognqiyingyong'));
                          // 先关闭底部弹窗
                          Navigator.pop(context);
                          // 延迟一小段时间，让用户看到提示信息
                          await Future.delayed(const Duration(seconds: 1));

                          // 重启应用
                          _restartApp();
                        },
                      );
                    },
                  );
                },
              ),


              const SizedBox(height: 30),

              ItemView(
                width: 360,
                height: 50,
                leftPadding: 20,
                rightPadding: 20,
                leftWidget: Row(
                  children: [
                    Text(
                      AppLocale.text('logout'),
                      style: const TextStyle(
                          fontSize: 15.0,
                          color: Colors.black
                      ),
                    ),
                  ],
                ),
                borderRadius: 8,
                onTap: _logout,
              ),
            ],
          )
      ),
    );
  }
}
